class Task29:
    def run(self, matrix):
        state = 0   # 0: right, 1: down, 2: left, 3: up
        rows, cols = matrix.shape
        i = 0
        j = -1
        result = []

        while True:
            if state == 0:
                if cols == 0:
                    break
                else:
                    for _ in range(cols):
                        j += 1
                        result.append(matrix[i][j])
                    rows -= 1
                    state = 1

            elif state == 1:
                if rows == 0:
                    break
                else:
                    for _ in range(rows):
                        i += 1
                        result.append(matrix[i][j])
                    cols -= 1
                    state = 2

            elif state == 2:
                if cols == 0:
                    break
                else:
                    for _ in range(cols):
                        j -= 1
                        result.append(matrix[i][j])
                    rows -= 1
                    state = 3

            elif state == 3:
                if rows == 0:
                    break
                else:
                    for _ in range(rows):
                        i -= 1
                        result.append(matrix[i][j])
                    cols -= 1
                    state = 0

        return result
